var ProjectParamList = {};
ProjectParamList.contextpath = "";
ProjectParamList.projectId="";
ProjectParamList.initPage = function(contextpath) {
	ProjectParamList.contextpath = contextpath;

	
	layui.use(['table','form'], function() {
		var table = layui.table;
		var form = layui.form;
		// 第一个实例
		table.render({
			elem : '#demo',
			toolbar : '#toolbarDemo'
			,
			url : '/web2019/project/projectParamList.action' // 数据接口
			,
			page : true // 开启分页
			,
			response : {
				statusName : 'code' // 规定数据状态的字段名称，默认：code
				,
				statusCode : "0000" // 规定成功的状态码，默认：0
				,
				msgName : 'message' // 规定状态信息的字段名称，默认：msg
				,
				countName : 'total' // 规定数据总数的字段名称，默认：count
			},
			cols : [ [ // 表头
			{
				field : 'id',
				title : 'id',
				width : 80,
				sort : true
			},{
				field : 'projectId',
				title : '项目id',
				hide  : true,
				width : 150,
				edit : 'text',
				sort : true
			}, {
				field : 'paramCode',
				title : '参数编码',
				width : 150,
				edit : 'text',
				sort : true
			},, {
				field : 'paramName',
				title : '参数名称',
				width : 150,
				edit : 'text',
				sort : true
			},{
				field : 'paramVal',
				title : '参数值',
				edit : 'text',
				width : 150
			},{
				field : 'parentParamId',
				title : '父参数',
				edit : 'text',
				width : 150
			}, {
				field : 'description',
				title : '描述',
				edit : 'text',
				width : 300,
				sort : true
			}, {
				title : '操作',
				templet : ProjectParamList.setOperate,
				width : 160,
				align : 'center',
				fixed : 'right'
			} ] ]
		});
		
		form.on('select(selectChange)', function(data){ 
			var projectId= $("#projectId").val();
			ProjectParamList.projectId=projectId;
			ProjectParamList.reloadTable(table,projectId);
			$("#projectId").val(projectId);
			layui.form.render('select');
		});
		
		// 监听单元格编辑
		table.on('tool(test)', function(obj) {
			if(obj.event == 'delete'){
		      layer.confirm('真的删除行么', function(index){
		        ProjectParamList.deleteParam(obj.data);
		        layer.close(index);
		      });
		    } else if(obj.event == 'add'){
		    	ProjectParamList.doAdd(obj.data)
		    }else if(obj.event == 'update'){
		    	ProjectParamList.doAdd(obj.data)
		    }
		});

	});
}
ProjectParamList.setOperate=function(data){
	var btnHtml = "";

	if (data.projectId != null) {
		btnHtml += '<button type="button" class="layui-btn layui-btn-xs" lay-event="update">更新</button>';
		btnHtml += '<button type="button" class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</button>';
	} else {
		btnHtml += '<button type="button" class="layui-btn layui-btn-xs" lay-event="add" >添加</button>';
	}
	return btnHtml;
}
ProjectParamList.reloadTable= function(table,projectId){
	table.reload('demo', {
        page: {
            curr: 1
        },
		where: 
		{
	     projectId :projectId
	    }
    });
}
ProjectParamList.doAdd =function(data){
	data.projectId =ProjectParamList.projectId;
	if(ProjectParamList.projectId==""){
		layer.message("请选择项目");
		return;
	}
	$.ajax({
		type : "POST",
		url : "/web2019/project/ajaxSaveProjectParam.action",
		dataType : "json",
		data : data,
		beforeSend:function(){
			layer.load(1);
		},
		complete:function(){
			layer.close(layer.index-1);
		},
		success : function(data) {
			if (data.code == "0000") {
				layer.alert(data.message, { icon: 1, closeBtn: 0 }, function (index) { 
					layer.close(index);
					var projectId= $("#projectId").val();
					layui.use(['table'], function() {
					var table = layui.table;
					table.reload('demo', {
						where : {
							"projectId" : projectId
						},
				    });
					});
					$("#projectId").val(projectId);
					
				});
			} else {
				layer.alert(data.message);
			}
		},
		error : function(data) {

		}
	});
}

ProjectParamList.deleteParam =function(data){
	$.ajax({
		type : "POST",
		url : "/web2019/project/ajaxDeleteProjectParam.action",
		dataType : "json",
		data : data,
		beforeSend:function(){
			layer.load(1);
		},
		complete:function(){
			layer.close(layer.index-1);
		},
		success : function(data) {
			if (data.code == "0000") {
				layer.alert(data.message, { icon: 1, closeBtn: 0 }, function (index) { 
					layer.close(index);
					var projectId= $("#projectId").val();
					layui.use(['table'], function() {
					var table = layui.table;
					table.reload('demo', {
						where : {
							"projectId" : projectId
						},
				    });
					});
					$("#projectId").val(projectId);
					
				});
			} else {
				layer.alert(data.message);
			}
		},
		error : function(data) {

		}
	});
}